#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
上传测试图片到COS
"""

from qcloud_cos import CosConfig, CosS3Client
import os

# COS配置
SECRET_ID = os.getenv('COS_SECRET_ID', '')
SECRET_KEY = os.getenv('COS_SECRET_KEY', '')
REGION = 'ap-beijing'
BUCKET = 'photoenhancei-bj-1259206939'

# 初始化COS客户端
config = CosConfig(Region=REGION, SecretId=SECRET_ID, SecretKey=SECRET_KEY)
client = CosS3Client(config)

# 要上传的测试图片
test_images = [
    ('test_images/test_image_1.jpg', 'test/test_image_1.jpg'),
    ('test-jpg/249KB.jpg', 'test/249KB.jpg'),
    ('test-jpg/458KB.jpg', 'test/458KB.jpg')
]

def upload_file(local_path, cos_key):
    """上传文件到COS"""
    full_local_path = os.path.join(os.path.dirname(__file__), local_path)
    
    if not os.path.exists(full_local_path):
        print(f"❌ 文件不存在: {full_local_path}")
        return None
    
    try:
        response = client.upload_file(
            Bucket=BUCKET,
            LocalFilePath=full_local_path,
            Key=cos_key,
            PartSize=1,
            MAXThread=10
        )
        
        # 生成COS URL
        cos_url = f"https://{BUCKET}.cos.{REGION}.myqcloud.com/{cos_key}"
        print(f"✅ 上传成功: {local_path}")
        print(f"   COS URL: {cos_url}")
        return cos_url
    except Exception as e:
        print(f"❌ 上传失败: {local_path}")
        print(f"   错误: {e}")
        return None

def main():
    """主函数"""
    print("🚀 开始上传测试图片到COS")
    print(f"📦 Bucket: {BUCKET}")
    print(f"🌍 Region: {REGION}")
    print("-" * 60)
    
    urls = {}
    for local_path, cos_key in test_images:
        url = upload_file(local_path, cos_key)
        if url:
            urls[cos_key] = url
    
    print("\n" + "="*60)
    print("📊 上传结果汇总")
    print("="*60)
    
    if urls:
        print(f"✅ 成功上传 {len(urls)}/{len(test_images)} 个文件\n")
        print("请将以下URL更新到测试脚本中：")
        print("-" * 60)
        for key, url in urls.items():
            print(f"# {key}")
            print(f'"{url}",')
        print("-" * 60)
    else:
        print("❌ 没有文件上传成功")
    
    return len(urls) == len(test_images)

if __name__ == "__main__":
    success = main()
    exit(0 if success else 1)

